package com.edu.neusoft.phone_sale.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.neusoft.phone_sale.common.ResponseResult;
import com.edu.neusoft.phone_sale.entity.Customers;
import com.edu.neusoft.phone_sale.service.CustomersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 客户信息表，记录所有客户的信息，便于销售管理和客户关系维护(Customers)表控制层
  */
@RestController
@Slf4j
@Api(value = "客户", tags = "客户")
@RequestMapping("/api/customers")
public class CustomersController {
    /**
     * 服务对象
     */
    @Resource
    private CustomersService customersService;


    /**
     * 分页查询客户信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询客户信息")
    @GetMapping("/page")
    public ResponseResult getByPage(int pageNum, int pageSize) {
        ResponseResult customerName = customersService.findByPage(pageNum, pageSize);
        return ResponseResult.okResult(customerName);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @GetMapping("{id}")
    public ResponseResult selectOne(@PathVariable Serializable id) {
        return ResponseResult.okResult(this.customersService.getById(id));
    }

    /**
     * 新增客户
     *
     * @param customers 实体对象
     * @return 新增结果
     */
    @ApiOperation("新增客户")
    @PostMapping
    public ResponseResult insert(@RequestBody Customers customers) {
        return ResponseResult.okResult(this.customersService.save(customers));
    }

    /**
     * 修改客户信息
     *
     * @param customers 实体对象
     * @return 修改结果
     */
    @ApiOperation("修改客户信息")
    @PutMapping
    public ResponseResult update(@RequestBody Customers customers) {
        return ResponseResult.okResult(this.customersService.updateById(customers));
    }

    /**
     * 根据id删除客户信息
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation("根据id删除客户信息")
    @DeleteMapping
    public ResponseResult delete(@RequestParam("idList") List<Long> idList) {
        return ResponseResult.okResult(this.customersService.removeByIds(idList));
    }
}

